<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
	  xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"
	  xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:pe="http://primefaces.org/ui/extensions">
<h:head>
	<title>Credit Scoring Application</title>
	<link rel="stylesheet" type="text/css" href="pages/style.css" />
	<meta http-equiv="pragma" content="no-cache"/>
	<script type="text/javascript"><!--//--><![CDATA[//><!--
		// Validate value of score
		function validateScore() {
			var score = jQuery('#frmDialogAddFactorChoice\\:score').val();
			var pattern = /^[1-9]+[0-9]*$/;
			return pattern.test(score);
		}

		// Validate value of weight field in form add or edit rule
		function validateWeight() {
			var weight = jQuery('#frmDialogAddRule\\:weight').val();
			// Pattern to validate with regular expression
			var pattern = /0.\d+/;
			return pattern.test(weight);
		}

		// Validate total weight of all rules of scheme
		function validateTotalWeight() {
			var length = jQuery('#frmDialogViewRule\\:numberOfRule').val();
			var totalWeight = 0.0;
			var ruleID = "";
			var ruleIDSelected = jQuery('#frmDialogAddRule\\:ruleID').val();

			// Loop list rule of schemeID
			for (var i=0; i<length; i++) {
				ruleID = document.getElementById("frmDialogViewRule:dtbRule:" + i +":ruleID").innerHTML
				// check which rule is selected
				if (ruleID != ruleIDSelected) {
					// Only caculate weight of un-selected rule
					totalWeight += parseFloat(document.getElementById("frmDialogViewRule:dtbRule:" + i +":weight")
													  .innerHTML);
				} else {
					// Weight of selected rule will be replace by weight of fied weight in form add or edit rule
					totalWeight += parseFloat(jQuery('#frmDialogAddRule\\:weight').val());
				}
			}
			return (parseFloat(totalWeight) <= 1);
		}
	//--><!]]></script>
</h:head>
<h:body>
	<h:form id="form">
		<!-- Context Menu -->
		<p:contextMenu for="schemeEnt">
	        <p:menuitem value="View Rule" icon="ui-icon-search"
	        			update=":frmDialogViewRule:dtbRule, :frmDialogViewRule:btnViewFactorChoice,
       							:frmDialogViewRule:btnDeleteRule, :frmDialogViewRule:btnEditRule,
       							:frmDialogViewRule:numberOfRule"
	        			actionListener="#{schemeBean.viewRuleOfScheme}" onclick="wdlgViewRule.show();"/>
	        <p:menuitem value="Delete" icon="ui-icon-close" onclick="wcfdDeleteScheme.show();"/>
	    </p:contextMenu>

		<!-- Datatable Scheme data -->
	    <p:dataTable id="schemeEnt" var="schemeEnt" value="#{schemeBean.listScheme}" rowKey="#{schemeEnt.schemeID}"
	    			 paginator="true" rows="10" editable="true"
	    			 selection="#{schemeBean.selectedScheme}" selectionMode="single">

			<!-- Master Header -->
			<f:facet name="header">LIST OF SCHEME</f:facet>

			<!-- Edit Scheme -->
			<p:ajax event="rowEdit" listener="#{schemeBean.onEditSchemeName}"
					update=":form:schemeEnt, :form:dlgSchemeNameDuplicate"/>
			<p:ajax event="rowEditCancel" update=":form:schemeEnt"/>

			<p:column style="width: 1%">
				<p:rowEditor/>
			</p:column>

			<!-- Scheme ID -->
	        <p:column headerText="Scheme ID" styleClass="scheme_table_header_id" sortBy="#{schemeEnt.schemeID}">
	            <p:cellEditor>
		        	<f:facet name="output">
						<h:outputText value="#{schemeEnt.schemeID}"/>
					</f:facet>
					<f:facet name="input">
						<h:inputText value="#{schemeEnt.schemeID}" label="Scheme ID" style="width: 100%; height: 19px;"
									 readonly="true"/>
						<h:inputHidden value="#{schemeEnt.schemeID}"></h:inputHidden>
					</f:facet>
				</p:cellEditor>
	        </p:column>

			<!-- Scheme Name -->
	        <p:column headerText="Scheme Name" sortBy="#{schemeEnt.schemeName}">
	        	<p:cellEditor>
		        	<f:facet name="output">
						<h:outputText value="#{schemeEnt.schemeName}"/>
					</f:facet>
					<f:facet name="input">
						<h:inputText value="#{schemeEnt.schemeName}" label="Scheme Name"
									 style="width: 100%; height: 19px;"/>
					</f:facet>
				</p:cellEditor>
	        </p:column>

	        <f:facet name="footer">
	        	<p:commandButton id="btnAddScheme" value="Add Scheme" onclick="wdlgAddScheme.show();" type="button"/>
	        </f:facet>
	    </p:dataTable>

	    <!-- Confirm Dialog before delete scheme -->
		<p:confirmDialog id="cfdDeleteScheme" message="#{msg.MSG_CONFIRM_DELETE_SCHEME}"
						 widgetVar="wcfdDeleteScheme" header="Initiating delete process" severity="alert">
			<p:commandButton id="btnConfirm" value="Yes Sure" onclick="wcfdDeleteScheme.hide()" process="@this"
							 actionListener="#{schemeBean.removeScheme}" update="schemeEnt, cfdDeleteEntireScheme"/>
			<p:commandButton id="btnDecline" value="Not Yet" onclick="wcfdDeleteScheme.hide()" type="button" />
		</p:confirmDialog>

		<!-- Confirm Dialog before delete entire scheme (rule, factor choice) -->
		<p:confirmDialog id="cfdDeleteEntireScheme" message="#{msg.MSG_CONFIRM_DELETE_ENTIRE_SCHEME}"
						 widgetVar="wcfdDeleteEntireScheme" header="Initiating delete process" severity="warning"
						 visible="#{requestScope.SHOW_MSG_SCHEME_HAS_RULE}">
			<p:commandButton id="btnConfirmEntire" value="Yes Sure" onclick="wcfdDeleteEntireScheme.hide()" process="@this"
							 actionListener="#{schemeBean.removeEntireScheme}" update="schemeEnt"/>
			<p:commandButton id="btnDeclineEntire" value="Not Yet" onclick="wcfdDeleteEntireScheme.hide()" type="button" />
		</p:confirmDialog>

		<!-- Dialog message when update scheme -->
	    <p:dialog id="dlgSchemeNameDuplicate" header="CS Error" modal="true" resizable="false" height="150" width="300"
	    		  widgetVar="wdlgSchemeNameDuplicate" visible="#{requestScope.DUPLICATE_SCHEME_NAME}">
		    <h:outputText value="#{msg.MSG_DUP_SCHEME_NAME}" />
		    <br/>
		    <br/>
		    <p:commandButton id="btnOK" value="OK" type="button" onclick="wdlgSchemeNameDuplicate.hide()"/>
		</p:dialog>
	</h:form>

	<!-- Form contains dialog to view rule -->
	<h:form id="frmDialogViewRule">
		<!-- Dialog Rule data -->
		<p:dialog id="dViewRule" modal="true" header="Rule Of Scheme" widgetVar="wdlgViewRule"
				  visible="#{ruleBean.visibleRule}" resizable="false" width="700" height="320">
			<p:dataTable id="dtbRule" var="ruleEnt" value="#{ruleBean.listRule}" rowKey="#{ruleEnt.ruleID}"
						 selection="#{ruleBean.selectedRule}" selectionMode="single" style="height: 250px;">
				<p:ajax event="rowSelect"
						update=":frmDialogViewRule:btnViewFactorChoice, :frmDialogViewRule:btnDeleteRule,
								:frmDialogViewRule:btnEditRule"/>
				<p:column headerText="Rule ID">
					<h:outputText id="ruleID" value="#{ruleEnt.ruleID}"/>
				</p:column>
				<p:column headerText="Factor Name">
					<h:outputText value="#{ruleEnt.factorName}"/>
				</p:column>
				<p:column headerText="Weight">
					<h:outputText id="weight" value="#{ruleEnt.weight}"/>
				</p:column>
			</p:dataTable>
			<h:inputHidden id="numberOfRule" value="#{ruleBean.numOfRecCurPage}"/>
			<br/>
			<p:commandButton id="btnViewFormAddRule" value="New" actionListener="#{ruleBean.viewFormAddRule}"
							 update=":frmDialogAddRule:dAddRule" oncomplete="wdlgAddRule.show();" />
			<p:commandButton id="btnViewFactorChoice" value="View" actionListener="#{ruleBean.getFactorChoice}"
							 update=":frmDialogViewFactorChoice:dFactorChoice" oncomplete="dlgViewFactorChoice.show();"
							 disabled="#{ruleBean.selectedRule.ruleID == null}"/>
			<p:commandButton id="btnEditRule" value="Edit" actionListener="#{ruleBean.viewFormEditRule}"
							 update=":frmDialogAddRule:dAddRule" oncomplete="wdlgAddRule.show();"
							 disabled="#{ruleBean.selectedRule.ruleID == null}"/>
			<p:commandButton id="btnDeleteRule" value="Delete" onclick="wcfdDeleteRule.show();"
							 disabled="#{ruleBean.selectedRule.ruleID == null}"/>
		</p:dialog>

		<!-- Confirm Dialog before delete rule -->
		<p:confirmDialog id="cfdDeleteRule" message="Are you sure about deleting the rule?"
						  widgetVar="wcfdDeleteRule" header="Initiating delete process" severity="alert">
			<p:commandButton id="btnConfirm" value="Yes Sure" onclick="wcfdDeleteRule.hide()" process="@this"
							 actionListener="#{ruleBean.removeRule}"
							 update=":frmDialogViewRule:dtbRule, :frmDialogViewRule:cfdDeleteEntireRule,
							 		 :frmDialogViewRule:btnViewFactorChoice, :frmDialogViewRule:btnDeleteRule"/>
			<p:commandButton id="btnDecline" value="Not Yet" onclick="wcfdDeleteRule.hide()" type="button" />
		</p:confirmDialog>

		<!-- Confirm Dialog before delete entire rule (factor choice) -->
		<p:confirmDialog id="cfdDeleteEntireRule" message="#{msg.MSG_CONFIRM_DELETE_ENTIRE_RULE}"
						 widgetVar="wcfdDeleteEntireRule" header="Initiating delete process" severity="warning"
						 visible="#{requestScope.SHOW_MSG_RULE_HAS_FACTORCHOICE}">
			<p:commandButton id="btnConfirmEntire" value="Yes Sure" onclick="wcfdDeleteEntireRule.hide()"
							 process="@this" actionListener="#{ruleBean.removeEntireRule}" update="dtbRule, numberOfRule"/>
			<p:commandButton id="btnDeclineEntire" value="Not Yet" onclick="wcfdDeleteEntireRule.hide()"
							 type="button" />
		</p:confirmDialog>
	</h:form>

	<!-- Form contains dialog to add new rule -->
	<h:form id="frmDialogAddRule">
		<p:dialog id="dAddRule" modal="true" widgetVar="wdlgAddRule" width="290" height="150" resizable="false"
				  header="#{sessionScope.IS_EDIT_RULE == true ? msg.MSG_EDIT_RULE : msg.MSG_NEW_RULE}">
			<h:panelGrid columns="2" style="margin-top:10px" cellpadding="4">
				<h:outputLabel for="cbbFactorName" value="Factor: " />
				<p:selectOneMenu id="cbbFactorName" value="#{factorBean.selectedFactor}" widgetVar="wdbbFactor">
		            <f:selectItems var="factorEnt" value="#{factorBean.listFactor}" itemLabel="#{factorEnt.factorName}"
		            			   itemValue="#{factorEnt.factorID}" />
		        </p:selectOneMenu>
		        <h:outputLabel for="weight" value="Weight: " />
		        <p:inputText id="weight" value="#{ruleBean.weight}" style="width: 100%;" size="10"/>
			</h:panelGrid>
			<h:inputHidden id="ruleID" value="#{ruleBean.selectedRule.ruleID}"/>
			<br/>
			<p:commandButton id="btnAddRule" actionListener="#{ruleBean.saveRule}" oncomplete="wdlgAddRule.hide();"
							 value="#{sessionScope.IS_EDIT_RULE == true ? msg.MSG_SAVE : msg.MSG_SUBMIT}"
							 process="@this, cbbFactorName, weight"
							 onclick="if (!validateWeight()) {wdlgMessageWeight.show(); return false;} else {if (!validateTotalWeight()) {wdlgMessageTotalWeight.show(); return false;}}"
							 update=":frmDialogAddRule:dlgFactorDuplicate, :frmDialogViewRule:dViewRule,
							 		 :frmDialogViewRule:numberOfRule"/>
			<p:commandButton id="btnViewFormAddFactor" value="New Factor" oncomplete="dlgAddFactor.show();"
							 update=":frmDialogViewRule:dtbRule"/>
		</p:dialog>

		<!-- Dialog message when update scheme -->
	    <p:dialog id="dlgFactorDuplicate" header="CS Error" modal="true" resizable="false" height="150" width="300"
	    		  widgetVar="wdlgFactorDuplicate" visible="#{requestScope.DUPLICATE_FACTOR}">
		    <h:outputText value="#{msg.MSG_DUP_FACTOR}" />
		    <br/>
		    <br/>
		    <p:commandButton id="btnOK" value="OK" type="button"
		    				 onclick="wdlgFactorDuplicate.hide(), wdlgViewRule.show(), wdlgAddRule.show()"/>
		</p:dialog>

		<!-- Dialog message when enter invalid weight -->
	    <p:dialog id="dlgMessageWeight" header="CS Error" modal="true" resizable="false" height="150" width="300"
	    		  widgetVar="wdlgMessageWeight">
		    <h:outputText value="#{msg.MSG_INVALID_WEIGHT}" />
		    <br/>
		    <br/>
		    <p:commandButton id="btnOKWeight" value="OK" type="button" onclick="wdlgMessageWeight.hide()"/>
		</p:dialog>

		<!-- Dialog message when enter invalid total weight -->
	    <p:dialog id="dlgMessageTotalWeight" header="CS Error" modal="true" resizable="false" height="150" width="300"
	    		  widgetVar="wdlgMessageTotalWeight">
		    <h:outputText value="#{msg.MSG_TOTAL_INVALID_WEIGHT}" />
		    <br/>
		    <br/>
		    <p:commandButton id="btnOKTotalWeight" value="OK" type="button" onclick="wdlgMessageTotalWeight.hide()"/>
		</p:dialog>
	</h:form>

	<!-- Form contains dialog to add new factor -->
	<h:form id="frmDialogAddFactor">
		<p:dialog id="dAddFactor" modal="true" header="New Factor" widgetVar="dlgAddFactor" resizable="false">
			<h:panelGrid columns="2" style="margin-top:10px">
				<h:outputLabel for="factorName" value="Factor Name: " />
		        <h:inputText id="factorName" value="#{factorBean.factorName}" style="width: 100%;"/>
			</h:panelGrid>
			<br/>
			<p:commandButton id="btnAddFactor" value="Submit" actionListener="#{factorBean.addFactor}"
							 process="@this, factorName" oncomplete="dlgAddFactor.hide();"
							 update=":frmDialogAddFactor:dlgFactorNameDuplicate, :frmDialogAddRule:cbbFactorName"/>
		</p:dialog>

		<!-- Dialog message when add factor -->
	    <p:dialog id="dlgFactorNameDuplicate" header="CS Error" modal="true" resizable="false" height="150" width="300"
	    		  widgetVar="wdlgFactorNameDuplicate" visible="#{requestScope.DUPLICATE_FACTOR_NAME}">
		    <h:outputText value="#{msg.MSG_DUP_FACTOR_NAME}" />
		    <br/>
		    <br/>
		    <p:commandButton id="btnOK" value="OK" type="button"
		    				 onclick="wdlgFactorNameDuplicate.hide(), dlgAddFactor.show()"/>
		</p:dialog>
	</h:form>

	<!-- Form contains dialog have factor choice -->
	<h:form id="frmDialogViewFactorChoice">
		<p:dialog id="dFactorChoice" modal="true" header="Rule Case" widgetVar="dlgViewFactorChoice"
				  width="700" height="320" resizable="false">
			<!-- Datatable contains factorchoice -->
			<p:dataTable id="dtbFactorChoice" var="factorChoiceEnt" value="#{factorChoiceBean.listFactorChoice}"
						 selection="#{factorChoiceBean.selectedFactorChoice}" selectionMode="single"
						 style="height: 250px;" rowKey="#{factorChoiceEnt.factorChoiceID}">

				<p:ajax event="rowSelect" update=":frmDialogViewFactorChoice:btnDeleteFactorChoice"/>

				<p:column headerText="Facor Choice ID">
					<h:outputText value="#{factorChoiceEnt.factorChoiceID}"/>
				</p:column>
				<p:column headerText="Factor Choice">
					<h:outputText value="#{factorChoiceEnt.factorChoice}"/>
				</p:column>
				<p:column headerText="Weight">
					<h:outputText value="#{factorChoiceEnt.score}"/>
				</p:column>
			</p:dataTable>
			<br/>
			<p:commandButton id="btnViewFormAddFactorChoice" value="New" oncomplete="dlgAddFactorChoice.show();"/>
			<p:commandButton id="btnDeleteFactorChoice" value="Delete" onclick="wcfdDeleteFactorChoice.show();"
							 disabled="#{factorChoiceBean.selectedFactorChoice.factorChoiceID == null}"/>
		</p:dialog>

		<!-- Confirm Dialog before delete factorchoice -->
		<p:confirmDialog id="cfdDeleteFactorChoice" message="Are you sure about deleting the factor choice?"
						  widgetVar="wcfdDeleteFactorChoice" header="Initiating delete process" severity="alert">
			<p:commandButton id="btnConfirm" value="Yes Sure" onclick="wcfdDeleteFactorChoice.hide()" process="@this"
							 actionListener="#{factorChoiceBean.removeFactorChoice}"
							 update=":frmDialogViewFactorChoice:dtbFactorChoice,
							 		 :frmDialogViewFactorChoice:btnDeleteFactorChoice"/>
			<p:commandButton id="btnDecline" value="Not Yet" onclick="wcfdDeleteFactorChoice.hide()" type="button" />
		</p:confirmDialog>
	</h:form>

	<!-- Form contains dialog to add new factor choice -->
	<h:form id="frmDialogAddFactorChoice">
		<p:dialog id="dAddFactorChoice" modal="true" header="New Rule Case" widgetVar="dlgAddFactorChoice"
				  width="330" height="150" resizable="false">
			<h:panelGrid columns="2" style="margin-top:10px" cellpadding="4">
				<h:outputLabel for="factorChoice" value="Factor Choice: " />
		        <h:inputText id="factorChoice" value="#{factorChoiceBean.factorChoice}" style="width: 100%;"/>
		        <h:outputLabel for="score" value="Sore: " />
		        <h:inputText id="score" value="#{factorChoiceBean.score}" style="width: 100%;"/>
			</h:panelGrid>
			<br/>
			<p:commandButton id="btnAddFactorChoice" value="Submit" actionListener="#{factorChoiceBean.addFactorChoice}"
							 process="@this, factorChoice, score" oncomplete="dlgAddFactorChoice.hide();"
							 update=":frmDialogViewFactorChoice:dtbFactorChoice,
							 		 :frmDialogAddFactorChoice:factorChoice, :frmDialogAddFactorChoice:score"
				 		 	 onclick="if (!validateScore()) {wdlgMessageScore.show(); return false;}"/>
		</p:dialog>

		<!-- Dialog message when score invalid -->
	    <p:dialog id="dlgMessageScore" header="CS Error" modal="true" resizable="false" height="150" width="300"
	    		  widgetVar="wdlgMessageScore">
		    <h:outputText value="#{msg.MSG_INVALID_SCORE}" />
		    <br/>
		    <br/>
		    <p:commandButton id="btnOKTotalWeight" value="OK" type="button" onclick="wdlgMessageScore.hide()"/>
		</p:dialog>
	</h:form>

	<!-- Form contains dialog to add scheme -->
	<h:form id="frmDialogAddScheme">
		<p:dialog id="dAddScheme" modal="true" header="Enter Scheme Name" widgetVar="wdlgAddScheme" resizable="false">
			<h:panelGrid columns="2" style="margin-top:10px">
				<h:outputLabel for="schemeName" value="Scheme Name: " />
				<p:inputText id="schemeName" value="#{schemeBean.schemeName}" />
			</h:panelGrid>
			<br/>
			<p:commandButton id="btnAddScheme" action="#{schemeBean.addScheme}" value="Submit"
							 update="schemeName, :form:schemeEnt, dlgSchemeNameDuplicate"
							 oncomplete="wdlgAddScheme.hide();"/>
		</p:dialog>

		<!-- Dialog message when add scheme -->
	    <p:dialog id="dlgSchemeNameDuplicate" header="CS Error" modal="true" resizable="false" height="150" width="300"
	    		  widgetVar="wdlgSchemeNameDuplicate" visible="#{requestScope.DUPLICATE_SCHEME_NAME}">
		    <h:outputText value="#{msg.MSG_DUP_SCHEME_NAME}" />
		    <br/>
		    <br/>
		    <p:commandButton id="btnOK" value="OK" type="button"
		    				 onclick="wdlgSchemeNameDuplicate.hide(), wdlgAddScheme.show()"/>
		</p:dialog>
	</h:form>
</h:body>
</html>